//
//  DSTestButton.m
//  Competition
//
//  Created by 段帅 on 16/4/14.
//  Copyright © 2016年 newfuture. All rights reserved.
//

#import "DSTestButton.h"
#import "SCUIToolHeader.h"
#define COLOR_BACKGROUND  [UIColor colorWithRed:248.0/255 green:248.0/255 blue:248.0/255 alpha:1.0f]


#define COLOR_TITLE_A5A5A5 [UIColor colorWithRed:165/255.0 green:165/255.0 blue:165/255.0 alpha:1]
#define COLOR_LINE_dbdbdb [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1]
#define FONE_MY(A) [UIFont systemFontOfSize:A*0.383]

@implementation DSTestButton

@synthesize  main_color=_main_color;
+(DSTestButton *)btnType:(UIButtonType)type{
    
    
    DSTestButton *button = [DSTestButton buttonWithType:type];
    
    button.selected = YES;
//    [button setBackgroundColor:COLOR_BACKGROUND];
   [button setBackgroundImage:[button imageWithColor:COLOR_BACKGROUND] forState:UIControlStateNormal];
    [button setBackgroundImage:[button imageWithColor:[UIColor whiteColor]] forState:UIControlStateSelected];
    [button setBackgroundImage:[button imageWithColor:Color_Main] forState:UIControlStateHighlighted];

    button.adjustsImageWhenHighlighted = NO;
    [button setTitle:@"获取验证码" forState:UIControlStateNormal];
    [button setTitleColor:COLOR_TITLE_A5A5A5 forState:UIControlStateNormal];
    [button setTitleColor:Color_Main forState:UIControlStateSelected];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    button.layer.borderWidth = 0.5;
    button.layer.cornerRadius = 3;
    button.layer.borderColor = COLOR_LINE_dbdbdb.CGColor;
    button.titleLabel.font = FONE_MY(30);
    [button addTarget:self  action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    return button;

    
    
    
    
    
}


//+(instancetype)buttonWithType:(UIButtonType)buttonType{
//    
//     DSTestButton *button = [DSTestButton buttonWithType:buttonType];
//    
//    button.selected = NO;
//    [button setBackgroundImage:[button imageWithColor:COLOR_BACKGROUND] forState:UIControlStateNormal];
//    [button setBackgroundImage:[button imageWithColor:[UIColor whiteColor]] forState:UIControlStateSelected];
//    [button setBackgroundImage:[button imageWithColor:COLOR_MAIN] forState:UIControlStateHighlighted];
//    
//    [button setTitle:@"获取验证码" forState:UIControlStateNormal];
//    [button setTitleColor:COLOR_TITLE_A5A5A5 forState:UIControlStateNormal];
//    [button setTitleColor:COLOR_MAIN forState:UIControlStateSelected];
//    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
//    button.layer.borderWidth = 0.5;
//    button.layer.cornerRadius = 3;
//    button.layer.borderColor = COLOR_LINE_dbdbdb.CGColor;
//    button.titleLabel.font = FONE_MY(30);
//    [button addTarget:self  action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
//
//    
//    
//    
//    return button;
//    
//}
-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
       [self createUI];
    }
    
    return self;
    
    
}
-(void)createUI{
    

    self.selected = YES;
    [self setBackgroundImage:[self imageWithColor:COLOR_BACKGROUND] forState:UIControlStateSelected];
    [self setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [self setBackgroundImage:[self imageWithColor:self.main_color] forState:UIControlStateHighlighted];
//  [self setBackgroundImage:[UIImage imageNamed:@"大赛简介.png"] forState:UIControlStateHighlighted];    
    [self setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self setTitleColor:COLOR_TITLE_A5A5A5 forState:UIControlStateSelected];
    [self setTitleColor:self.main_color forState:UIControlStateNormal];
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
     self.layer.borderWidth = 0.5;
    self.layer.cornerRadius = 3;
    self.layer.borderColor = COLOR_LINE_dbdbdb.CGColor;
    self.titleLabel.font = FONE_MY(30);
   // [self addTarget:self  action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
//    self.adjustsImageWhenHighlighted = NO;
}

-(void)clickButton:(UIButton *)button{
    //点击获得验证码
    
    
    [self setTitle:@"正在获取.." forState:UIControlStateNormal];
    NSString *phone = self.PhoneBlock();
    NSString *type = self.typeBlock();
    
    
    NSString    * strSysVersion = [[UIDevice currentDevice] systemVersion];                    // 手机系统版本号
    NSString    * phoneModel    = [[UIDevice currentDevice] model];                            // 手机型号
    NSString    * UUID          = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; // 唯一标识码
    
        NSMutableDictionary * parameters = [NSMutableDictionary dictionary];
    [parameters setValue:phone    forKey:@"mobile"];
    [parameters setValue:@"3"          forKey:@"type"];
    [parameters setValue:phoneModel    forKey:@"dlsbxh"];
    [parameters setValue:strSysVersion forKey:@"dlxtbb"];
    [parameters setValue:UUID          forKey:@"yjbsm"];
    NSLog(@"%@",parameters);
    
    
    
    
//    [DSHttpManager POST:URL_MAIN_WITH(URL_USER_SMS) parameters:dic isToken:YES finishBlock:^(NSDictionary *dataDic) {
//        NSLog(@"%@",dataDic);
//        
//        if ([dataDic[@"status"] intValue] == 0) {
////            [[JLActionManager sharedInstance]showHUDWithLabelText:@"短信已发送，请注意查收" imageView:SucceedImage inView:self];
//            
//            self.isCanUse = NO;
//            _timeMiao = 120;
//            __weak id weakSelf = self;
//            self.displayLink = [CADisplayLink displayLinkWithTarget:weakSelf selector:@selector(timer)];    
//            [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//            self.displayLink.frameInterval = 60;
//            if (self.sucessdBlock != NULL) {
//                  self.sucessdBlock(@"验证码已发送，请注意查收");
//            }
//          
//            
//        }else{
//            
//             [self setTitle:@"获取验证码" forState:UIControlStateNormal];
//            if (self.faildBlock != NULL) {
//
//                self.faildBlock(dataDic[@"statusCode"]);}
//            
////            [[JLActionManager sharedInstance]showHUDWithLabelText:dataDic[@"statusCode"] imageView:SucceedImage inView:self];
//            
//            
//        }
//        
//       
//        
//    } faildeBlock:^(NSError *error) {
//        NSLog(@"%@",error);
//        
//    }];
//    
   

// _dsTimer =  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer) userInfo:nil repeats:YES];
//    
    
    
    
}
-(void)timer{
    
    
    if (_timeMiao <= 0) {
       
        self.isCanUse = YES;
        
        [self setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.displayLink invalidate];
        self.displayLink = nil;
        
    }else{
         _timeMiao=_timeMiao-1;
        NSLog(@"%ld",_timeMiao);
        [self setTitle:[NSString stringWithFormat:@"%ld秒后重发",_timeMiao] forState:UIControlStateNormal];
        
        
    }
    
    
    
    
}



-(void)setIsCanUse:(BOOL)isCanUse{
    if (isCanUse) {
        self.selected = NO;
        self.userInteractionEnabled = YES;
        self.layer.borderColor = self.main_color.CGColor;
       
    }else{
        
        self.selected = YES;
        
        
        self.userInteractionEnabled = NO;
        self.layer.borderColor = COLOR_LINE_dbdbdb.CGColor;
        
    }
    
    
    
}


- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


- (UIImage *)imageByApplyingAlpha:(CGFloat)alpha  image:(UIImage*)image

{
    
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);
    
    
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);
    
    
    
    CGContextScaleCTM(ctx, 1, -1);
    
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    
    
    
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    
    
    
    CGContextSetAlpha(ctx, alpha);
    
    
    
    CGContextDrawImage(ctx, area, image.CGImage);
    
    
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    
    
    UIGraphicsEndImageContext();
    
    
    
    return newImage;
    
}

-(UIColor *)main_color{
    
    if (!_main_color) {
        return Color_Main;
    }
    return _main_color;
}
-(void)setMain_color:(UIColor *)main_color{
    _main_color = main_color;
    [self setBackgroundImage:[self imageWithColor:main_color] forState:UIControlStateHighlighted];
     [self setTitleColor:main_color forState:UIControlStateNormal];
}

@end
